function tmap(IN_FILE, COND1, COND2)

% Put 3 files (tmap.m, channel102.txt, data.mat) in the current directory
% of Matlab, and type
% > tmap('data.mat', 1, 5)
% This will show t-maps of L1 vs. R1 in 6 frequency bands.
% One in lower left panel (A-B ord 3, 8-30 Hz) corresponds to Figure 5A of 
% Noguchi, Y. & Kakigi, R. NeuroImage 222 (2020) 117294 
% 
% COND1, COND2: 
% sequental numbers of conditions that should be comapred by t-test.
% (1:L1, 2:L2, 3:L4, 4:L6, 5:R1, 6:R2, 7:R4, 8:R6)


load(IN_FILE);

[CH_NUM, BAND_NUM, COND_NUM, SUB_NUM, SEN_TYPE_NUM] = size(IPI_LEN_MS_AVE);

IPI_LEN_MS_AVE(8, 1:BAND_NUM, 1:COND_NUM, 1:SUB_NUM, 1:SEN_TYPE_NUM) = ( IPI_LEN_MS_AVE(5, 1:BAND_NUM, 1:COND_NUM, 1:SUB_NUM, 1:SEN_TYPE_NUM) + ...
    IPI_LEN_MS_AVE(7, 1:BAND_NUM, 1:COND_NUM, 1:SUB_NUM, 1:SEN_TYPE_NUM) + IPI_LEN_MS_AVE(55, 1:BAND_NUM, 1:COND_NUM, 1:SUB_NUM, 1:SEN_TYPE_NUM) +...
    IPI_LEN_MS_AVE(59, 1:BAND_NUM, 1:COND_NUM, 1:SUB_NUM, 1:SEN_TYPE_NUM) )/4; 

T_VALUE = zeros(CH_NUM,BAND_NUM);

for ch = 1:CH_NUM
    for bn = 1:BAND_NUM
        COND1_DATA(1:SUB_NUM) = IPI_LEN_MS_AVE(ch, bn, COND1, 1:SUB_NUM, 3);
        COND2_DATA(1:SUB_NUM) = IPI_LEN_MS_AVE(ch, bn, COND2, 1:SUB_NUM, 3);
        DIFF(1:SUB_NUM) = COND1_DATA(1:SUB_NUM) - COND2_DATA(1:SUB_NUM) ;
        T_VALUE(ch,bn) = mean(DIFF) / (std(DIFF)/sqrt(SUB_NUM));
    end
end


fidr = fopen('channel102.txt', 'r');
if fidr == -1
    fprintf(1, 'Error: channel102.txt is necessary to draw a t-contour map\n');
    return
end

X_POS = zeros(CH_NUM,1);
Y_POS = zeros(CH_NUM,1);

for ch = 1:CH_NUM
    dummy = fscanf(fidr, '%d\t', 1);
    X_POS(ch,1) = fscanf(fidr, '%d\t', 1);
    Y_POS(ch,1) = fscanf(fidr, '%d\n', 1);
end


% Draw contour map ---------------------------------------

ti = 0:1:200;
[XI,YI] = meshgrid(ti,ti);

Fig_H = 0.30;% 0.13
Fig_V = 0.40;


contourdata1 = griddata(X_POS(1:102,1), Y_POS(1:102,1), T_VALUE(1:102, 1), XI, YI);
subplot('position',[0.05 0.54 Fig_H Fig_V]), contourf(contourdata1);colormap(jet);colorbar('vert'); caxis([-3 3]);

TITLE_STR = strcat(COND_NAME{COND1,1},'vs',COND_NAME{COND2,1}, ' ', FREQ_LABEL{3,1}, ':', num2str(CUT_FREQ(1,1)), '-',  num2str(CUT_FREQ(1,2)), ' Hz.');
title(TITLE_STR, 'Fontsize', 12);

contourdata2 = griddata(X_POS(1:102,1), Y_POS(1:102,1), T_VALUE(1:102, 2), XI, YI);
subplot('position',[0.37 0.54 Fig_H Fig_V]), contourf(contourdata2);colormap(jet);colorbar('vert'); caxis([-3 3]);
TITLE_STR = strcat(FREQ_LABEL{4,1}, ':  ', num2str(CUT_FREQ(2,1)), ' - ',  num2str(CUT_FREQ(2,2)), ' Hz.');
title(TITLE_STR, 'Fontsize', 12);

contourdata3 = griddata(X_POS(1:102,1), Y_POS(1:102,1), T_VALUE(1:102, 3), XI, YI);
subplot('position',[0.69 0.54 Fig_H Fig_V]), contourf(contourdata3);colormap(jet);colorbar('vert'); caxis([-3 3]);
TITLE_STR = strcat(FREQ_LABEL{5,1}, ':  ', num2str(CUT_FREQ(3,1)), ' - ',  num2str(CUT_FREQ(3,2)), ' Hz.');
title(TITLE_STR, 'Fontsize', 12);

contourdata4 = griddata(X_POS(1:102,1), Y_POS(1:102,1), T_VALUE(1:102, 4), XI, YI);
subplot('position',[0.05 0.04 Fig_H Fig_V]), contourf(contourdata4);colormap(jet);colorbar('vert'); caxis([-3 3]);
TITLE_STR = strcat(FREQ_LABEL{6,1}, ':  ', num2str(CUT_FREQ(4,1)), ' - ',  num2str(CUT_FREQ(4,2)), ' Hz.');
title(TITLE_STR, 'Fontsize', 12);

contourdata5 = griddata(X_POS(1:102,1), Y_POS(1:102,1), T_VALUE(1:102, 5), XI, YI);
subplot('position',[0.37 0.04 Fig_H Fig_V]), contourf(contourdata5);colormap(jet);colorbar('vert'); caxis([-3 3]);
TITLE_STR = strcat(FREQ_LABEL{7,1}, ':  ', num2str(CUT_FREQ(5,1)), ' - ',  num2str(CUT_FREQ(5,2)), ' Hz.');
title(TITLE_STR, 'Fontsize', 12);

contourdata6 = griddata(X_POS(1:102,1), Y_POS(1:102,1), T_VALUE(1:102, 6), XI, YI);
subplot('position',[0.69 0.04 Fig_H Fig_V]), contourf(contourdata6);colormap(jet);colorbar('vert'); caxis([-3 3]);
TITLE_STR = strcat(FREQ_LABEL{8,1}, ':  ', num2str(CUT_FREQ(6,1)), ' - ',  num2str(CUT_FREQ(6,2)), ' Hz. Target period: ', num2str(TGT_INT_SEC(1,1)), ' to ', num2str(TGT_INT_SEC(1,2)), ' sec');
title(TITLE_STR, 'Fontsize', 12);



fclose('all');

